//滑动窗口+正难则反：找子序列之和等于sum-x的最长序列
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        // 求数组全部元素的和
        for (auto e : nums) {
            sum += e;
        }
        int target = sum - x;
        if (target < 0)
            return -1;
        int tmp = 0;
        int len = -1; // 可能没有符合子序列存在
        for (int left = 0, right = 0; right < nums.size(); right++) {
            tmp += nums[right];
            while (tmp > target) {
                tmp -= nums[left++];
            }
            if (tmp == target)
                len = max(len, right - left + 1);
        }
        if (len == -1)
            return -1;
        else
            return nums.size() - len;
    }
};